var jwt          = require('jsonwebtoken');
var moment       = require('moment');
var db           = require('../../config/mongo_database');
var tokenManager = require('../../config/token_manager');
var logger       = require('../../config/log4js').logger('access');


// app: 首页活动列表
exports.appActlist = function(req, res){
    db.activityModel.find({status:'1', hot:'1', isend:'1'}, 
      {name:1, type:1, price:1, btype:1, img:1, member:1, totalm:1, start:1}, function(err, list){
        res.json({status:200, list:list, msg:'查询成功'}); 
    })
}


// app: 首页活动列表
exports.actlist = function(req, res){
    var  isend    = req.body.isend     || '1';// 0已开奖, 1进行中， 2待开奖
    var  where    = {};
    var  returns  = { name:1, type:1, price:1, btype:1, img:1, member:1, total:1, totalm:1, start:1, end:1}

    if(isend!=''){
      if(isend == '0'){
        where.isend     = '0';  // 活动结束
        where.actstatus = '1';  // 已开奖
        where.status    = '1'
        console.log(where)
        db.activityModel.find(where, returns, function(err, list){
          return res.json({status:200, list:list, msg:'查询成功'}); 
        })
      }
      else if(isend == '1'){ 
        where.isend  = '1'   // 活动未结束
        where.status = '1'
        where.end    = {"$gt": moment(new Date()).format("YYYYMMDDHHmm")}
        db.activityModel.find(where, returns, function(err, list){
          return res.json({status:200, list:list, msg:'查询成功'}); 
        })
      }else if(isend == '2'){
        where.isend  = '0'       // 活动已结束
        where.actstatus = '0';   // 未开奖
        where.status = '1'       
        // where.end    = {"$lt": moment(new Date()).format("YYYYMMDDHHmm")}
        db.activityModel.find(where, returns, function(err, list){
          return res.json({status:200, list:list, msg:'查询成功'}); 
        })
      }
    }        
}

// 活动详情
exports.actdetail = function(req, res){
    var id = req.query.id || '';

    db.activityModel.findOne({_id: id}, 
      {
        member:    1,
        totalm:    1,
        name:      1,
        type:      1,
        price:     1,
        btype:     1,
        remark:    1,
        result:    1,
        img:       1,
        resultlen: 1,
        fee:       1,
        total:     1,
        content:   1,
        actstatus: 1,
        start:     1,
        end:       1,
        isend:     1
      },function(err, detail){
        if(err || detail==null)
          return res.json({status:400, msg:'未查询到数据'}); 
        else
          return res.json({status:200, detail:detail, msg:'查询成功'}); 
    })
}